package com.whut.controller;

import com.whut.model.ProblemDescription;
import com.whut.model.ParPrefixSystem;
import com.whut.service.NewPrefixSystemService;
import com.whut.utils.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/newSystem")
public class NewSystemController {

    @Resource
    private NewPrefixSystemService newprefixsystemservice;


    @ResponseBody
    @PostMapping("/importSystem")
    public String importSystem(@RequestParam("file") MultipartFile file) {
        Date date = new Date();
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
        System.out.println(time);
        String resultPath = null;
        try {
            /*
                将压缩包文件 上传到服务器
                路径为
                    D:\\apache-tomcat-8.5.41\\webapps\\yirui_sc_war \\upload    \\files\\
                返回
                    D:\\apache-tomcat-8.5.41\\webapps\\yirui_sc_war\\upload\\files\\
                file.getOriginalFilename()返回文件名（包括后缀）
             */
            resultPath = FileUtil.upload(file.getInputStream(), "files/", time + file.getOriginalFilename());
            System.out.println(resultPath);
            File f = new File(resultPath + time + file.getOriginalFilename());
            /*
                解压文件
                将上述返回的路径作为解压的路径
                把压缩包的文件名作为解压后的文件夹的名字
             */
            String newFileName = time + file.getOriginalFilename().split("\\.")[0];
            FileUtil.unZipFiles(f, resultPath, newFileName);
            return newprefixsystemservice.importSystem(resultPath + newFileName);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @ResponseBody
    @RequestMapping("/exportSystem")
    public String exportSystem() {
        return newprefixsystemservice.exportExcel();
    }


    @ResponseBody
    @RequestMapping("/getFirstLevel")
    public List<ParPrefixSystem> getFirstLevel() {
        return newprefixsystemservice.getFirstLevel();
    }

    /**
     * 已知preNum，获取当前深度
     *
     * @date: 2019/5/30 15:11
     */
    @ResponseBody
    @RequestMapping("/getNextLevel")
    public List<ParPrefixSystem> getNextLevel(int preNum) {
        return newprefixsystemservice.getNextLevel(preNum);
    }

    @ResponseBody
    @RequestMapping("/getNextLevelExceptDeprecated")
    public List<ParPrefixSystem> getNextLevelExceptDeprecated(int preNum) {
        return newprefixsystemservice.getNextLevelExceptDeprecated(preNum);
    }


    @ResponseBody
    @RequestMapping("/getLastLevel")
    public List<ProblemDescription> getLastLevel(int preNum) {
        return newprefixsystemservice.getLastLevel(preNum);
    }

@ResponseBody
    @RequestMapping("/getLastLevelExceptDeprecated")
    public List<ProblemDescription> getLastLevelExceptDeprecated(int preNum) {
        return newprefixsystemservice.getLastLevelExceptDeprecated(preNum);
    }


    @ResponseBody
    @RequestMapping("/insertOne")
    public String insertOne(ParPrefixSystem ParPrefixSystem) {
        return newprefixsystemservice.addPrefixSystem(ParPrefixSystem);
    }

    @ResponseBody
    @RequestMapping("/insertLastOne")
    public String insertLastOne(ProblemDescription Newproblemdescription) {
        return newprefixsystemservice.addProblemdescription(Newproblemdescription);
    }

    @ResponseBody
    @RequestMapping("/delMidOne")
    public String delMidOne(int number) {
        return newprefixsystemservice.delMidOne(number);
    }

    @ResponseBody
    @RequestMapping("/delLastOne")
    public String delLastOne(int number) {
        return newprefixsystemservice.delLastOne(number);
    }

    @ResponseBody
    @RequestMapping("/updateMid")
    public String updateMid(ParPrefixSystem ParPrefixSystem) {
        return newprefixsystemservice.updateMid(ParPrefixSystem);
    }

    @ResponseBody
    @RequestMapping("/updateLast")
    public String updateLast(ProblemDescription Newproblemdescription) {
        return newprefixsystemservice.updateLast(Newproblemdescription);
    }

    @ResponseBody
    @RequestMapping("/addAdvice")
    public String addAdvice(int number, String advice) {
        return newprefixsystemservice.addAdvice(number, advice);
    }


    /**
     * 废弃当前级别和下一级，废弃最后级
     *
     * @param number
     * @return java.lang.String
     * @author liyvanliang
     * @date 2019/8/2 15:04
     */
    @ResponseBody
    @RequestMapping("/discMidOne")
    public String discMidOne(int number) {
        return newprefixsystemservice.discMidOne(number);
    }

    @ResponseBody
    @RequestMapping("/discLastOne")
    public String discLastOne(int number) {
        return newprefixsystemservice.discLastOne(number);
    }

    /**
     * 为语音关键词录入界面做体系数据准备
     *
     * @return java.lang.Object
     * @author fanyu
     * @date 2019/11/19 10:57
     * @params []
     */
    @ResponseBody
    @RequestMapping("/getAllSystemInfo")
    public Object getAllSystemInfo() {
        return newprefixsystemservice.getAllSystemInfo();
    }

    @ResponseBody
    @PostMapping("/getFirstStageSystemInfo")
    public Object getFirstSystemInfo(Integer nodeDepth) {
        return newprefixsystemservice.getSomeSystemInfoByNodeDepth(nodeDepth);
    }

    /**
     * 关键词映射界面信息搜索
     *
     * @return java.lang.Object
     * @author fanyu
     * @date 2019/11/19 16:34
     * @params [system]
     */
    @ResponseBody
    @RequestMapping("/getProblemDescriptionById")
    public Object getProblemDescriptionById(String system) {
        return newprefixsystemservice.getProblemDescriptionById(system);
    }

}
